package it.rikesh.aip2011.client.component;

import java.util.logging.Logger;

import it.rikesh.aip2011.client.service.HomeService;
import it.rikesh.aip2011.client.service.HomeServiceAsync;

import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.Text;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;

/**
 * Classe che implementa la homepage del sito. Si occupa di ricavare dal datastore tutte le informazioni necessarie.
 * @author  RN
 */
public class HomeLayout extends LayoutContainer{
    private static final Logger log = Logger.getLogger(HomeLayout.class.getName());
    private static HomeServiceAsync homeService = GWT.create(HomeService.class);
    
    private Text name;
    private Text txt;
    private HTML image;
    
    public HomeLayout(){
        init();
        getInfoHome();
        addComponent();
    }
    
    /**
     * Inizializzo le variabili
     */
    public void init(){
        
          
        image = new HTML();
        name=new Text("MONTE ROSA");
        name.setStyleAttribute("color", "red");
        name.setStyleAttribute("text-align", "center");
        name.setStyleAttribute("font-weight", "bold");
        name.setStyleAttribute("font-size", "35px");
        name.setStyleAttribute("font-family", "Rock Salt");
        txt=new Text();
    }
    
    /**
     * Permettere di ottenere tutti dati nel datatstore relativo alla homepage
     */
    private void getInfoHome(){
        homeService.getHomeImg("home", new AsyncCallback<String>() {
            
            public void onSuccess(String result) {
//                img.setUrl("images/"+result);
                image.setHTML("<center><img src='images/"+result+"'/></center>");
                layout();
                
            }
            
            public void onFailure(Throwable caught) {
                log.warning("Home: Errore nel callback di img - "+caught.getMessage());
                
            }
        });
        
        homeService.getHomeDescription("home", new AsyncCallback<String>() {
            
            public void onSuccess(String result) {
                txt.setText(result);
                layout();
            }
            
            public void onFailure(Throwable caught) {
                log.warning("Home: Errore nel callback di description");
                
            }
        });
    }
    
    public void addComponent(){
//        add(name);
//        add(img);
//        add(txt);
        removeAll();
        add(name);  
        add(image);  
        add(txt); 
        layout();
    }

}
